
//
//  TopUpViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "TopUpViewController.h"
#import "TopUpPreferentialCell.h"
#import "PaymentCell.h"
#import "LGImageManagerIO.h"
#import "UserInfoModel.h"
NSString * const TopUpPreferentialCellIdentifier = @"TopUpPreferentialCellIdentifier";
NSString * const PaymentCellIdentifier = @"PaymentCellIdentifier";
@interface TopUpViewController ()
@property(nonatomic, assign) NSInteger preferentialIndex;
@property(nonatomic, assign) NSInteger paymentIndex;
@property(nonatomic, strong) NSArray * rechargeList;
@property(nonatomic, copy) NSString * pay_type;
@property(nonatomic, copy) NSString * total_price;
@end

@implementation TopUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle: @"充值"];
    [self setupBottomView];
    self.preferentialIndex = -1;
    self.paymentIndex = 0;
    self.pay_type = @"1";
    [self requestRecharge];
}
- (void)requestRecharge{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/getRechargeList" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            strongSelf.rechargeList = responseObject[@"data"];
        }
        [strongSelf.tableView reloadData];
    }];
    
}


- (void)setupBottomView{
    CGFloat bottomHeight = 50;
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0,  self.view.frame.size.height-bottomHeight, self.view.frame.size.width, bottomHeight)];
    [bottomView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:bottomView];
    
    UIButton * paymentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [paymentBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    [paymentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [paymentBtn setFrame:CGRectMake(20, 5, self.view.frame.size.width -40, bottomHeight- 10)];
    paymentBtn.layer.cornerRadius = (bottomHeight- 10)/2;
    paymentBtn.layer.masksToBounds =  YES;
    paymentBtn.titleLabel.font = [UIFont systemFontOfSize:15];
     [paymentBtn addTarget:self action:@selector(paymentAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:paymentBtn];
    
    UIImage *paymentBtnBgImg = [LGImageManagerIO createGradualColorImage:paymentBtn.size startColor:UIColorFromRGB(0xE1CFA9) endColor:UIColorFromRGB(0xC1B284)];
    [paymentBtn setBackgroundImage:paymentBtnBgImg forState:UIControlStateNormal];
   

}
- (void)paymentAction{
   

    
    [self submitTopUp];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height   -50);
    return tableViewFrame;
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TopUpPreferentialCell class]) bundle:nil] forCellReuseIdentifier:TopUpPreferentialCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PaymentCell class]) bundle:nil] forCellReuseIdentifier:PaymentCellIdentifier];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 0;
    if (indexPath.section == 0) {
        height = 50;
    }else if (indexPath.section == 1){
        
        height = 60;
    }
    return height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 0;
    if (section == 0) {
        rows = [self.rechargeList count];
    }else if (section == 1){
        rows = [[self getPaymentArray] count];
    }
    return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.section == 0) {
        TopUpPreferentialCell * tempCell = [tableView dequeueReusableCellWithIdentifier:TopUpPreferentialCellIdentifier];
        BOOL selectedState = NO;
        if (self.preferentialIndex == indexPath.row ) {
            selectedState = YES;
        }
        [tempCell setupInfo:self.rechargeList[indexPath.row]];
        [tempCell setupPreferentialSelected:selectedState];
        cell = tempCell;
    }else if (indexPath.section == 1){
        PaymentCell * tempCell = [tableView dequeueReusableCellWithIdentifier:PaymentCellIdentifier];
        BOOL state =  NO;
        if (self.paymentIndex == indexPath.row) {
            state = YES;
        }
        [tempCell setupPaymentData:[self getPaymentArray][indexPath.row]];
        [tempCell setupPaymentSelectedState:state];
        cell = tempCell;
    }
 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSArray *)getPaymentArray{
    NSArray * tempArray = @[@{@"img":@"zhifu_zhifubao",@"title":@"支付宝支付"},@{@"img":@"zhifu_weiixn",@"title":@"微信支付"},@{@"img":@"zhifu_yinghangka",@"title":@"银联支付"}];
    return tempArray;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        self.preferentialIndex = indexPath.row;
        self.total_price = self.rechargeList[indexPath.row][@"price"];
    }else if (indexPath.section == 1){
        self.paymentIndex = indexPath.row;
        if (indexPath.row ==0) {
            self.pay_type = @"1";
        }else if (indexPath.row == 1){
            self.pay_type = @"2";
        }else if (indexPath.row == 2){
            self.pay_type = @"3";
        }
       
    }
    [self.tableView reloadData];
}

- (void)submitTopUp{
    
    if (!self.pay_type ||!self.total_price) {
        return;
    }
    /**
     
     token    是    string    用户token
     pay_type    否    int    支付方式 1-支付宝 2-微信 3-银联支付 (默认不传为1)
     total_price    是    float    充值金额
     platform    否    int    平台 0-APP 1-小程序 (默认不传为0)
     */
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    
    [params addEntriesFromDictionary:@{@"pay_type":self.pay_type}];
    [params addEntriesFromDictionary:@{@"total_price":self.total_price}];
    [params addEntriesFromDictionary:@{@"platform":@"0"}];
    
    
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/ref" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
               [strongSelf showAlertView:@"充值成功,返回我的钱包" withConfirmAction:^(UIAlertAction *action) {
                   [[NSNotificationCenter defaultCenter] postNotificationName:@"UPATE_WALLET_NOTIFI" object:nil];
                   [strongSelf backViewController];
                   
               }];
        }
        
    }];
    
}
@end
